#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Shows basic usage of the RAMS 'traffic' and 'traffic-type' resources. """

import time
import rawsc
import rawsc.service
from rawsc import rams
import rawsc.rams.service
try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

USER = "xxx"
PWD = "xxx"
SERVER = "rams.mon01.rambla.be"

try:
    # Create a Rass instance, passing it your login credentials and the base service uri
    client = rawsc.rams.service.RamsService(username=USER, password=PWD, server=SERVER)
    
    # get list of format profiles
    qry = rawsc.Query()
    qry["kind"] = "file"
    qry["type"] = "http,rtmp"
    qry["from"] = "2010-09-01"
    qry["until"] = "2010-09-31"
    feed = client.getTrafficFeed(query = qry)
    print "\nRetrieved feed of traffic entries:"
    for e in feed.entry:
        print "\nEntry URI: " + e.id.text
        print "Kind = %s, Type = %s" % (e.kind, e.type)
        print "Hits: " + e.content.params.hits.text
        print "Volume: " + str(e.content.params.volume.text)
        print "path: " + e.content.params.path.text
        print "updated: " + e.content.params.updated.text
        print "last_request: " + str(e.content.params.last_request.text)
        if e.kind == "file":
            print "pct_0_10: " + str(e.content.params.pct_0_10.text)
            print "pct_10_20: " + str(e.content.params.pct_10_20.text)
            print "pct_20_30: " + str(e.content.params.pct_20_30.text)
            print "pct_30_40: " + str(e.content.params.pct_30_40.text)
            print "pct_40_50: " + str(e.content.params.pct_40_50.text)
            print "pct_50_60: " + str(e.content.params.pct_50_60.text)
            print "pct_60_70: " + str(e.content.params.pct_60_70.text)
            print "pct_70_80: " + str(e.content.params.pct_70_80.text)
            print "pct_80_90: " + str(e.content.params.pct_80_90.text)
            print "pct_90_100: " + str(e.content.params.pct_90_100.text)
            print "pct_unknown: " + str(e.content.params.pct_unknown.text)
            print "time0: " + str(e.content.params.time0.text)
            print "time1: " + str(e.content.params.time1.text)
            print "time2: " + str(e.content.params.time2.text)
            print "time3: " + str(e.content.params.time3.text)
            print "time4: " + str(e.content.params.time4.text)
            print "time5: " + str(e.content.params.time5.text)
            print "time6: " + str(e.content.params.time6.text)
            print "time7: " + str(e.content.params.time7.text)
            print "time8: " + str(e.content.params.time8.text)
            print "sec_total: " + str(e.content.params.sec_total.text)
            print "sec_avg: " + str(e.content.params.sec_avg.text)

    # get a list of traffic types
    feed = client.getTrafficTypeFeed()
    for e in feed.entry:
        print "\nEntry URI: " + e.id.text
        print "name: " + e.content.params.name.text
        print "description: " + str(e.content.params.description.text)
        print "product: " + e.content.params.product.text
    

except rawsc.service.Error, e:
    print "Error Response from server, contents = %s.\n" % str(e)
    error_dict = e[0] # error dict inside the first tuple elem
    print "status code : " + str(error_dict["status"]) + "\n"
    if error_dict.has_key("body"):
        print "error body : " + error_dict["body"] + "\n"
except rawsc.RawscException, e:
    print "RawscException caught, reason = %s.\n" % str(e)
except StandardError, e:
    print "StandardError caught, reason = %s.\n" % str(e)
except:
    print "unhandled exception caught\n"
